package com.eascs.ticket.hooks.svn.controller;

import java.util.regex.Pattern;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.common.util.StringUtil;
import com.eascs.site.controller.BaseViewController;
import com.eascs.site.view.ViewContext;
import com.eascs.ticket.hooks.svn.form.PreCommitForm;

@Controller
public class PreCommitController extends BaseViewController {

	private static Logger logger = LoggerFactory.getLogger(PreCommitController.class);

	@RequestMapping("/hooks/svn/pre-commit/verify")
	public String verify(PreCommitForm preCommitForm) {
		logger.info("verify:{}", preCommitForm.toString());
		String errorMessage = "Please submit format of message as: common-10020 add ValueFilter";
		String message = "success";
		if (!checkMessage(preCommitForm.getMessage())) {
			message = errorMessage;
		}
		ViewContext.put("message", message);
		return "/common/message";
	}

	private boolean checkMessage(String message) {
		if (StringUtil.isEmpty(message)) {
			return false;
		}
		String patternString = "^\\w{2,}\\-\\d+\\s+\\S+(.*\\r*\\n*.*)*";
		Pattern pattern = Pattern.compile(patternString);
		return pattern.matcher(message).matches();
	}

	public static void main(String[] args) {
		PreCommitController preCommitController = new PreCommitController();
		System.out.println(preCommitController.checkMessage("ops-10914 git迁移到svn"));
		System.out.println(preCommitController.checkMessage("ops-10914	 	git迁移到svn"));
		System.out.println(preCommitController.checkMessage("Oops-10914 "));
		System.out.println(preCommitController.checkMessage("Oops-10914 git迁移到svn"));
		System.out.println(preCommitController.checkMessage(" ops-10914  "));
		System.out.println(preCommitController.checkMessage("ops-10914 git迁移到svn\n ops-10914 git迁移到svn"));
		System.out.println(preCommitController.checkMessage("ops-10914 git迁移到svn\n ops-10914 git迁移到svn\n ops-10914 git迁移到svn"));
		System.out.println(preCommitController.checkMessage("ops-10914 git迁移到svn\n ops-10914 git迁移到svn\n ops-10914 git迁移到svn\n\t ops-10914 git迁移到svn"));
		System.out.println(preCommitController.checkMessage("common-10020	add ValueFilter"));
	}

}
